package com.tanhua.manager.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.manager.domain.UserStatus;
import com.tanhua.manager.mapper.UserStatusMapper;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;

/**
 * @Author : wangpengfei
 * @Description :
 * @Date : 2021/3/1 18:04
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class UserStatusJob {

    @Autowired
    private UserStatusMapper userStatusMapper;

    @Reference
    private UserInfoApi userInfoApi;

//    @Scheduled(cron = "0 */10 * * * ?")
    @Scheduled(cron = "0/5 * * * * ?")
    private void job(){
        System.out.println("查看是否有需要解封的账户");
        QueryWrapper<UserStatus> wrapper = new QueryWrapper<>();
        wrapper.lt("un_freeze_time",new Date());
        List<UserStatus> userStatusList = userStatusMapper.selectList(wrapper);
        if(userStatusList==null){
            return;
        }
        for (UserStatus userStatus : userStatusList) {
            UserInfo userInfo = userInfoApi.findById(userStatus.getUserId().longValue());
            userInfo.setUserStatus("1");
            userInfoApi.update(userInfo);

            userStatusMapper.deleteById(userStatus.getId());
        }
    }
}
